local meta = FindMetaTable("Player")

sql.Query([[
		CREATE TABLE IF NOT EXISTS rpdmmoney (Money NUMERIC, SteamID TEXT)
		]])

function meta:GetMoney()
	return sql.QueryValue(string.format([[
		SELECT Money FROM rpdmmoney WHERE SteamID = '%s']], 
		self:SteamID()
	))
end

function meta:SetMoney(amt)
	if amt == nil then
		debug.Trace()
		amt = 0
	end
	sql.Query(string.format([[
		UPDATE rpdmmoney SET Money = %s WHERE SteamID = '%s']], 
		amt, self:SteamID()
	))
	
	self:SetDTInt(DT_INT_MONEY, self:GetMoney())
end

function meta:GiveMoney(amt)
	/*
	if (self:GetAFK() && amt > 0) then
		amt = -amt
		Notify(self, 1, 3, "You are AFK!, you just lost $" .. -amt .. "!")
	end
	*/
	if amt == nil then
		debug.Trace()
		amt = 0
	end
	sql.Query(string.format([[
		UPDATE rpdmmoney SET Money = Money + %s
		WHERE SteamID = '%s']], 
		amt, self:SteamID()
	))
	
	self:SetDTInt(DT_INT_MONEY, self:GetMoney())
end

function meta:TakeMoney(amt)
	if amt == nil then
		debug.Trace()
		amt = 0
	end
	sql.Query(string.format([[
		UPDATE rpdmmoney SET Money = Money - %s
		WHERE SteamID = '%s']], 
		amt, self:SteamID()
	))
	
	self:SetDTInt(DT_INT_MONEY, self:GetMoney())
end

function meta:CanAfford(cost)
	if cost == nil then
		debug.Trace()
		cost = 0
	end
	return tonumber(self:GetMoney()) >= cost
end

function meta:CheckForNewPlayer()
	if !self:GetMoney() then
		sql.Query(string.format([[
			INSERT INTO rpdmmoney (Money, SteamID) VALUES (%s, '%s')]], 
			100000, self:SteamID()
		))
	end
end
